﻿using HibernatingRhinos.Profiler.Appender.NHibernate;
using NHibernate;
using NHibernate.Cfg;
using NhibernateDemoCore;

namespace NhibernateDemoConsole
{
    internal class Program
    {
        private static void Main()
        {
            NHibernateProfiler.Initialize();

            var configuration = new Configuration().Configure();

            using (var factory = configuration.BuildSessionFactory())
            {
//                new HqlStatements().Execute(factory);
                //                new CreateCustomer().Execute(factory);
                //                new CreateCustomerWithoutTransaction().Execute(factory);
                //                new CreateCustomerWithOrder().Execute(factory);
                //                new LoadCustomer().Execute(factory);
                //                new GetCustomerTwice().Execute(factory);
                                new BulkLoadCustomers().Execute(factory);
            }

            NHibernateProfiler.Stop();
        }
    }

    internal class HqlStatements
    {
        public void Execute(ISessionFactory factory)
        {
            using (var session = factory.OpenSession())
            {
                using (var tx = session.BeginTransaction())
                {
                    try
                    {
                        var customer = new Customer
                                           {
                                               Name = "Specialty Bakers, Inc.",
                                               BillingAddress = new Address
                                                                    {
                                                                        Line1 = "450 S State Rd",
                                                                        City = "Marysville",
                                                                        State = "PA",
                                                                        Zip = "17053"
                                                                    }
                                           };
                        customer.CreateOrder("widget");

                        var id = session.Save(customer);
                        tx.Commit();
                    }
                    catch
                    {
                        tx.Rollback();
                        throw;
                    }
                }
            }
            using (var session = factory.OpenSession())
            {
                using (var tx = session.BeginTransaction())
                {
                    try
                    {
                        var customers = session
                            .CreateQuery("from Customer")
                            .Future<Customer>();
                        var customer = session.CreateQuery("from Customer where id =1001").FutureValue<Customer>();

                        var name = customer.Value.Name;
                        tx.Commit();
                    }
                    catch
                    {
                        tx.Rollback();
                        throw;
                    }
                }
            }
        }
    }
}